package com.sctvcloud.zhaohua_sd.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.FComment;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.AnchorCommentDynamicsHolder;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.AnchorHomeDynamicsHolder;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.BaseAbsHolder;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.CommentHolder;
import com.sctvcloud.zhaohua_sd.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/26 0026.
 */

public class CommentDetailAdapter extends BaseHolderAbsAdapter<IListShowData, BaseAbsHolder<IListShowData>> {

    public static final int TYPE_TOP = 301;

    private OnItemInternalClick playClick;

    private String anchorId;

    private OnItemInternalClick internalPlayClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            if (playClick != null) {
                playClick.onItemInternalClick(itemView, view, position);
            }
        }
    };

    public OnItemInternalClick getPlayClick() {
        return playClick;
    }

    public void setPlayClick(OnItemInternalClick playClick) {
        this.playClick = playClick;
    }

    public CommentDetailAdapter(Context context, List<IListShowData> list) {
        super(context, list);
    }

//    public CommentDetailAdapter(Context context, List<? extends IListShowData> list) {
//        super(context, null);
//        addDatasEx(list);
//    }

    @Override
    public BaseAbsHolder<IListShowData> onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseAbsHolder bah = null;
        if (viewType == TYPE_TOP) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_anchor_comment_dynamics, null);
            bah = new AnchorCommentDynamicsHolder(context, view, null);
            bah.setInternalClick(internalPlayClick);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_topic_detail_comment, null);
            bah = new CommentHolder(context, view, null);
            bah.setAnchorId(anchorId);
            bah.setInternalClick(this);
        }
        return bah;
    }

    @Override
    public int getItemViewType(int position) {
        IListShowData da = getItem(position);
        if (da != null) {
            return da.getDataViewType();
        }
        return super.getItemViewType(position);
    }

    @Override
    public CommentDetailAdapter setItemInternalClick(OnItemInternalClick itemInternalClick) {
        super.setItemInternalClick(itemInternalClick);
        return this;
    }

    @Override
    public int addData(IListShowData iListShowData) {
        if (iListShowData != null) {
            if (data == null) {
                data = new ArrayList<>();
            }
            int insert = data.size();
            data.add(iListShowData);
            notifyItemInserted(insert);
            notifyDataSetChanged();
            return insert;
        }
        return -1;
        // return super.addData(iListShowData);
    }

    @Override
    public int addData(IListShowData t, int pos) {
        if (t == null) {
            return -1;
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        int count = data.size();
        if (pos < 0 || pos >= count) {
            pos = count;
            data.add(t);
        } else {
            data.add(pos, t);
        }
        notifyItemInserted(pos);
        notifyDataSetChanged();
        return pos;
    }

    public void setAnchorId(String anchorId) {
        this.anchorId = anchorId;
    }
}
